import numpy as np
import pandas as pd
import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from coommon import * #激活函数

def get_data():
    #获取数据
    data = pd.read_csv('../data/train.csv')
    #归一化
    X = data.drop(['label'], axis=1)
    y = data['label']
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
    #特征工程
    scaler = MinMaxScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)

    return X_test,y_test

def init_network():
    #直接从文件中加载参数
    network = joblib.load('../data/nn_sample')
    return network

def forward(network, x):
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']
    #第一层
    a1 = np.dot(x, W1) + b1

    z1 = sigmoid(a1)
    #第二层
    a2 = np.dot(z1, W2) + b2
    z2 = sigmoid(a2)
    #第三层
    a3 = np.dot(z2, W3) + b3
    y = softmax(a3)
    return y

y_pred = forward(init_network(),get_data()[0])

y_pred = np.argmax(y_pred, axis=1) # 获取概率最大的元素的索引

#计算分类准确率
accuracy = np.sum(y_pred == get_data()[1]) / len(y_pred)
print("分类准确率：", accuracy)